package app.client.file.copys;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.channels.FileChannel;

public class FileRename {
	
	private String filePath = "";

	public String getFilePath() {
		return filePath;
	}

	public void setFilePath(String filePath) {
		this.filePath = filePath;
	}
	
	public void renameFile(){
		
		File parentFolder = new File(this.filePath);
		for(File parent:parentFolder.listFiles()){
			if(parent.isDirectory()){
				for(File child:parent.listFiles()){
					
					System.out.println("开始处理："+child.getPath().replaceAll("\\\\", "/"));
					if(getTotalSizeOfFilesInDir(child)>0){
						if(child.getPath().endsWith("（已完成）")){
							continue;
						}
						String newPath = child.getPath().replaceAll("\\\\", "/")+"（已完成）";
						child.renameTo(new File(newPath));
					}else{
						System.out.println(getTotalSizeOfFilesInDir(child));
						System.out.println(child.getPath().replaceAll("\\\\", "/"));
						child.delete();
					}
				}
			}	
		}
		
	}
	

    private long getTotalSizeOfFilesInDir(final File file) {
        if (file.isFile())
            return file.length();
        final File[] children = file.listFiles();
        long total = 0;
        if (children != null)
            for (final File child : children)
                total += getTotalSizeOfFilesInDir(child);
        return total;
    }
	
	public static void main(String[] args) throws IOException {
		FileRename rename = new FileRename();
		rename.filePath = "E:/My_Project/dataEmpire/WebRoot/file/txt";
		rename.renameFile();
	}
}
